10. System-Level IO {CSAPP}

README

์ด ํŒŒ์ผ์€ ์‚ฌ์‹ค 11. Network Programming {CSAPP}, ์›น ์„œ๋ฒ„ ๋งŒ๋“œ๋Š” ๋ฐ ํ•„์š”ํ•œ RIO ํŒจํ‚ค์ง€ ๋•Œ๋ฌธ์— ๋„˜์–ด์™”๋‹ค. ํŒŒ์ผ ๋””์Šคํฌ๋ฆฝํ„ฐ์™€ ์ž…๋ ฅ์ถœ๋ ฅ์— ๋Œ€ํ•œ ๋‚ด์šฉ์„ ๋น ๋ฅด๊ฒŒ ์งš๊ณ  ๋ฐ”๋กœ RIO ํŒจํ‚ค์ง€ ๊ตฌํ˜„์ฒด๋กœ ๋„˜์–ด๊ฐˆ ๊ฒƒ. ๋Œ€์ถฉ ๋ดค๋Š”๋ฐ ๋ฒ„ํผ๊ฐ€ ์—†์–ด ๋น ๋ฅด๋‹ค๊ณ ?

์œ ๋‹‰์Šค ์„ธ์ƒ์—์„œ ๋ชจ๋“  ๊ฒƒ์€ ํŒŒ์ผ์ด๋‹ค.

๋Œ€ํ•™์‹œ์ ˆ ๊ต์ˆ˜๋‹˜์ด ๊ท€์— ๋”ฑ์ง€๊ฐ€ ์•‰๋„๋ก ๋ง์”€ํ•˜์…จ๋‹ค. ๊ทธ๋ž˜์„œ ํŒŒ์ผ์„ ์–ด๋–ป๊ฒŒ ๋‹ค๋ฃจ๋Š”์ง€, file descriptor๊ฐ€ ๋ฌด์—‡์ธ์ง€, ํŒŒ์ผ ๋ฉ”ํƒ€๋ฐ์ดํ„ฐ๋ฅผ ์–ด๋–ป๊ฒŒ ์“ฐ๋Š”์ง€ ๋“ฑ๋“ฑ์— ๋Œ€ํ•œ ๋‚ด์šฉ์„ 0015.2 Systems Programming {ssu2021-1st} ๐Ÿผ ์ˆ˜์—…์‹œ๊ฐ„์— ๋“ค์—ˆ๋‹ค.

mmap์€ ์œ ๋‹‰์Šค ์„ธ์ƒ์—์„œ ์Šค์œ„์Šค ์•„๋ฏธ ๋‚˜์ดํ”„์ด๋‹ค.

์ž์„ธํ•œ ๊ฑด 11. memory mapping and Copy-on-write (COW) {SP}์—์„œ ํ™•์ธ๋ฐ”๋žŒ.

์Šคํฌ๋ฆฐ์ƒท 2023-09-14 ์˜คํ›„ 9.19.57.png|800

10.5. Robust Reading and Writing with the RIO Package

๋‘ ๊ฐ€์ง€ ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•œ๋‹ค.

  1. Unbuffered Input & output: ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ˆ˜์ค€์˜ ๋ฒ„ํผ ์—†์ด ๊ณง์žฅ fd๋ฅผ ์‚ฌ์šฉํ•ด ํŒŒ์ผ์„ ์ฝ๊ฑฐ๋‚˜ ์“ธ ์ˆ˜ ์žˆ๋‹ค.

    1. ssize_t rio_readn(int fd, void *usrbuf, size_t n);
    2. ssize_t rio_writen(int fd, void *usrbuf, size_t n);
    3. Returns: number of bytes transferred if OK, 0 on EOF (rio_readn only), โˆ’1 on error
  2. Buffered Input: thread safeํ•œ ๋ฒ„ํผ๊ธฐ๋ฐ˜ ์ž…๋ ฅ์„ ํ†ตํ•ด ์˜ค๋ฒ„ํ—ค๋“œ๋ฅผ ์ค„์ธ๋‹ค.

    1. ssize_t rio_readlineb(rio_t *rp, void *usrbuf, size_t maxlen);
    2. ssize_t rio_readnb(rio_t *rp, void *usrbuf, size_t n);
    3. Returns: number of bytes read if OK, 0 on EOF, โˆ’1 on error

void rio_readinitb(rio_t *rp, int fd);: rio ๊ฐ์ฒด ์ดˆ๊ธฐํ™”, file descriptor ํ•„์š”.

read(2)

10.6. Readig File Metadata

stat(2) ์ฐธ์กฐ

#include <unistd.h> 
#include <sys/stat.h> 
int stat(const char *filename, struct stat *buf); 
int fstat(int fd, struct stat *buf); 
// Returns: 0 if OK, โˆ’1 on error

struct stat *์— ํŒŒ์ผ ๋ฉ”ํƒ€๋ฐ์ดํ„ฐ๊ฐ€ ๋‹ด๊ธด๋‹ค.

/* Metadata returned by the stat and fstat functions */ 
struct stat { 
	dev_t st_dev; /* Device */ 
	ino_t st_ino; /* inode */ 
	mode_t st_mode; /* Protection and file type */ 
	nlink_t st_nlink; /* Number of hard links */ 
	uid_t st_uid; /* User ID of owner */ 
	gid_t st_gid; /* Group ID of owner */ 
	dev_t st_rdev; /* Device type (if inode device) */ 
	off_t st_size; /* Total size, in bytes */ 
	unsigned long st_blksize; /* Block size for filesystem I/O */ 
	unsigned long st_blocks; /* Number of blocks allocated */ 
	time_t st_atime; /* Time of last access */ 
	time_t st_mtime; /* Time of last modification */ 
	time_t st_ctime; /* Time of last change */
};
๊ทธ๋Ÿฐ๋ฐ, ํŒŒ์ผ ๋ฉ”ํƒ€๋ฐ์ดํ„ฐ๋Š” ์‚ฌ์šฉ์ž๊ฐ€ ์ž„์˜๋Œ€๋กœ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ๋Š”๊ฑฐ ์•„๋‹ˆ์—ˆ์Œ?

10.7. Read Directory Contents

#include <sys/types.h> 
#include <dirent.h> 
DIR *opendir(const char *name);
// Returns: pointer to handle if OK, NULL on error

directory stream์˜ ์ฒซ๋ฒˆ์งธ ์ฃผ์†Œ๋ฅผ ๊ฐ€๋ฆฌํ‚จ๋‹ค. ํ•ด๋‹น ๋””๋ ‰ํ† ๋ฆฌ ์›์†Œ๊ฐ€ ๊ณ ๊ฐˆ๋  ๋•Œ๊นŒ์ง€ readdir์„ ํ˜ธ์ถœํ•˜๋ฉด ๋‹ค์Œ struct dirent * ํฌ์ธํ„ฐ๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ๋‹ค.

#include <dirent.h> 
struct dirent *readdir(DIR *dirp); 
// Returns: pointer to next directory entry if OK, NULL if no more entries or error

DIR * ๊ฐ์ฒด๋ฅผ ๋‹ซ์„ ๊ฒฝ์šฐ closedir์„ ํ˜ธ์ถœํ•˜๋ฉด ๋œ๋‹ค.

10.8. Sharing Files

I/O Redirection

์•„์ฃผ ์ค‘์š”ํ•œ ๊ฐœ๋…์ธ dup2์— ๋Œ€ํ•ด์„œ ๋‚˜์˜จ๋‹ค. ๋ฆฌ๋ˆ…์Šค ์…ธ์—์„œ๋Š” >, 2>, <๊ฐ€ ์žˆ๋‹ค. ๊ฐ๊ฐ

  1. lhs > rhs: lhs์˜ stdout ์ถœ๋ ฅ ๊ฒฐ๊ณผ๋ฅผ rhsํŒŒ์ผ๋กœ ๋ฆฌ๋””๋ ‰์…˜ ํ•˜์‹œ์˜ค.
  2. lhs 2> rhs: lhs์˜ stderr ์ถœ๋ ฅ ๊ฒฐ๊ณผ๋ฅผ rhs ํŒŒ์ผ๋กœ ๋ฆฌ๋””๋ ‰์…˜ ํ•˜์‹œ์˜ค.
  3. lhs < rhs: rhsํŒŒ์ผ์„ lhs์˜ stdin์œผ๋กœ ๋ฆฌ๋””๋ ‰์…˜ ํ•˜์‹œ์˜ค.
#include <unistd.h> 
int dup2(int oldfd, int newfd); 
// Returns: nonnegative descriptor if OK, โˆ’1 on error

descriptor table์˜ newfd๋ฒˆ์งธ ์ธ๋ฑ์Šค์˜ ๊ฐ’์„ oldfd์˜ ๊ฐ’์œผ๋กœ ๋ฎ์–ด์“ด๋‹ค. ๋งŒ์•ฝ newfd๊ฐ€ ์—ด๋ ค์žˆ๋‹ค๋ฉด ์ž๋™์œผ๋กœ ๋‹ซ๋Š”๋‹ค.